package org.inote.controller;


import org.inote.common.KeyStatus;
import org.inote.pojo.tablePojo.Note;
import org.inote.pojo.tablePojo.User;
import org.inote.service.NoteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 该控制器用于，当用户点击某个链接请求时，查询请求页面所需的数据
 */
@Controller
public class MainConroller {



    @Resource
    private NoteService noteService;

    /**
     * 用户请求我的主页:用户信息，用户所写的所有笔记
     * @param session
     * @return
     */
    @RequestMapping("/enterMyHome")
    public String enterMyHome(HttpSession session, HttpServletRequest request){
        User user = (User) session.getAttribute(KeyStatus.USER_INFO);
        if(user == null){
            return "login";
        }
        Integer userId = user.getUserId();
        List<Note> noteList = noteService.queryUserAllNotes(userId);
        //将用户笔记信息存入request作用域中
        request.setAttribute(KeyStatus.USER_NOTES,noteList);
        return "myhome";
    }


    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute(KeyStatus.USER_INFO);
        return "redirect:enterMain";
    }


    /**
     * 用户请求主页
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/enterMain")
    public String enterMain(HttpServletRequest request,HttpSession session){

        //查询所有用户笔记
        List<Note> allNotes = noteService.queryAllNotes();
        request.setAttribute(KeyStatus.ALL_NOTES,allNotes);
        //查询精华笔记

        return "index";
    }

    @RequestMapping("/enterOtherHome")
    public String enterOtherHome(HttpSession session,HttpServletRequest request){
        return "otherHome";
    }


}
